package com.qimou.iems.controller;


import com.qimou.iems.entity.PageArgs;
import com.qimou.iems.entity.PageList;
import com.qimou.iems.entity.User;
import com.qimou.iems.exception.InterfaceCommonException;
import com.qimou.iems.service.IUserService;
import com.qimou.iems.util.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Resource
    private IUserService userService;



    @RequestMapping(value = "/getUserList")
    public ViewDataPage getUserList(PageArgs pageArgs){
        Map<String,Object> map = new HashMap<String, Object>();
        PageList<User> userList = userService.getUserList(map, pageArgs);
        return buildSuccessViewDataPage(StatusConstant.SUCCESS_CODE,"获取成功",
                userList.getTotalSize(),userList.getList());
    }



    @RequestMapping(value = "save",method = RequestMethod.POST)
    public ViewData save(User user){
        if (CommonUtil.isEmpty(user.getRoles())){
            return buildFailureJson(StatusConstant.ARGUMENTS_EXCEPTION,"参数错误");
        }
        try {
            userService.save(user);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return buildFailureJson(StatusConstant.Fail_CODE,"操作失败");
        }
        return buildSuccessCodeJson(StatusConstant.SUCCESS_CODE,"操作成功");
    }





}
